<?
class CitySearchBuilder {
	
	public function getReviews($storeId, $lat, $lng, $searchTerm) {
		
		$storeNameNode ="business_name";
		$citySearchKey="10000001970";
	  $citySearchAPIUrl="http://api.citygridmedia.com/content/reviews/v2/search/latlon?";
		$url = $citySearchAPIUrl . "lat=" . $lat . "&" . "lon=" . $lng . 
		"&radius=5&what=" . $searchTerm . "&publisher=" .  $citySearchKey;
		$curl = curl_init();
		curl_setopt ($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$result = curl_exec ($curl);
		curl_close ($curl);
		
		$xmlString =<<<XML
$result
XML;

		$initArray = array();
		$dom = new DOMDocument();
		$dom->loadXML($xmlString);
	  
		foreach ($dom->childNodes as $node) {
			foreach ($node->childNodes as $node2) {
				if($node2->nodeName =="reviews"){
					
					foreach($node2->childNodes as $reviewNode){
						$article = new ArticleVO();	
						foreach($reviewNode->childNodes as $detailNode){
							if($detailNode->nodeName == "review_title")
							  $article->title = $detailNode->nodeValue;
							  
							if($detailNode->nodeName == "review_text")
							  $article->excerpt = $detailNode->nodeValue;
							  
							if($detailNode->nodeName == "review_author")
							  $article->user = $detailNode->nodeValue;
							  
							if($detailNode->nodeName == "source")
							  $article->source = $detailNode->nodeValue;
							  
							  
							if($detailNode->nodeName == "business_name")
							  $article->preFilterStoreName = $detailNode->nodeValue;
							  
							if($detailNode->nodeName == "review_author_url")
							  $article->userUrl = $detailNode->nodeValue;
							  
							if($detailNode->nodeName == "review_url")
							  $article->articleUrl = $detailNode->nodeValue;
							  
							 $article->storeId =$storeId;
							  
							 	
						}
					array_push($initArray, $article);
					}
				}
			} 
		}
		
		$filters =$this->getFilters($storeId);
		$articles = array();
		//print "Int count: "  . count($initArray);
		foreach($initArray as $article){
			if($this->hasFilterItem($article->preFilterStoreName, $filters)){
				array_push($articles, $article);
			}
		}
		
		return $articles;
	}
	
  private function getFilters($id) {
	  $sql="select filter from result_filters where store_id =" . $id;
	  $q = mysql_query($sql);
	  $result = array();
    while($row = mysql_fetch_row($q))
    {
    	array_push($result, $row[0]);
    }
  	return $result;
  }
  
  private function hasFilterItem($testCase, $arrayOfValues) {
  	//print "Test Case: " . $testCase . "<br>";
  	$isGood = false;
  	foreach($arrayOfValues as $value){
  		//print "Filter: " . $value  . "<br>";
  		//print "Str Pos: " . strpos(strtolower($testCase), strtolower($value)) . "<br>";
  		if(
  		  !(strpos(strtolower($testCase), strtolower($value)) === false)
  		 ){
  			//print "Set isGood <br>";
  			$isGood = true;
  			break;
  		}
  	}
  	return $isGood;
  }
	
}

?>